
//Definition for singly-linked list.
class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}


//合并两个有序链表
class Solution3 {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode newNode = new ListNode();
        ListNode th = newNode;
        while (list1 != null && list2 != null) {
            if (list1.val < list2.val) {
                th.next = list1;
                list1 = list1.next;
            } else {
                th.next = list2;
                list2 = list2.next;
            }
            th = th.next;
        }
        if (list1 != null) {
            th.next = list1;
        }
        if (list2 != null) {
            th.next = list2;
        }
        return newNode.next;
    }

}
